﻿using DetectingKinectSencor._40_Tools;
using DetectingKinectSencor._50_Evaluation;
using DetectingKinectSencor._50_Evaluation.ViewModels.Dashboard;
using DetectingKinectSencor.Tools;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Shapes;

using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using OxyPlot;
using System.Windows.Navigation;
using WPFPageSwitch;
using DetectingKinectSencor;


namespace WPFPageSwitch
{
	public partial class Dashboard : ISwitchable
	{
        private SQLiteConnection databaseConnection;
        private string databaseName { get; set; }

        private DateTime lastElementCaptureTime;

        public TrackingSequences savedCapturesList { get; set; }

        private ViewingDirectionViewModelDashboard viewingDirectionViewModelDashboard { get; set; }
        private MovementViewModelDashboard movingViewModelDashboard { get; set; }
        private GesturesViewModelDashboard gesturesViewModelDashboard { get; set; }
        private SlidesViewModelDashboard slidesChangeViewModelDashboard { get; set; }
        private WordsPerMinuteViewModelDashboard WordsPerMinuteViewModelDashboard { get; set; }
        private MultimediaViewModelDashboard videoDetectedViewModelDashboard { get; set; }

        private TimeSpan totalLength;

        public Dashboard(String databaseName)
		{
            this.databaseName = databaseName;

			InitializeComponent();

		}


        public void initializeDashboard()
        {
            // Open DB connection
            databaseConnection = SqliteConnection.openDBConnection(databaseName);

            // Loads the list with the entrys when the tracking was started and returns this list
            savedCapturesList = EvaluationData.loadList(databaseConnection);

            if (savedCapturesList.startCapture.Count == 0)
            {
                MessageBox.Show("There is no sequence in the DB found!");
                return;
            }
            else
            {
                // In the initialization the last element of the capture list will be previewed
                // -1 because the index starts at 0
                lastElementCaptureTime = savedCapturesList.startCapture[savedCapturesList.startCapture.Count - 1];
            }

            // Preview the total lengh of the presentation on the UI
            totalLength = savedCapturesList.endCapture[savedCapturesList.endCapture.Count - 1]
                          - savedCapturesList.startCapture[savedCapturesList.startCapture.Count - 1];
                         

            totalLengthLBL.Content = totalLength;

            // Update the list in the window
            updateSaveCaptureLST();

            // Get the data from the DB and save them to the static lists
            EvaluationData.loadData(databaseConnection, savedCapturesList.startCapture.Count);

            // Close the DB connection
            SqliteConnection.closeConnection(databaseConnection);



            viewingDirectionViewModelDashboard = new ViewingDirectionViewModelDashboard(lastElementCaptureTime);
            movingViewModelDashboard = new MovementViewModelDashboard(lastElementCaptureTime);
            gesturesViewModelDashboard = new GesturesViewModelDashboard(lastElementCaptureTime);
            slidesChangeViewModelDashboard = new SlidesViewModelDashboard(lastElementCaptureTime);
            videoDetectedViewModelDashboard = new MultimediaViewModelDashboard(lastElementCaptureTime);
            WordsPerMinuteViewModelDashboard = new WordsPerMinuteViewModelDashboard(lastElementCaptureTime);

            // Set the data context
            ViewingDirectionGraph.DataContext = viewingDirectionViewModelDashboard;
            MovingGraph.DataContext = movingViewModelDashboard;
            GesturesGraph.DataContext = gesturesViewModelDashboard;
            SlideChangeGraph.DataContext = slidesChangeViewModelDashboard;
            MultimediaGraph.DataContext = videoDetectedViewModelDashboard;
            WordsPerMinuteGraph.DataContext = WordsPerMinuteViewModelDashboard;
        }


        public void updateSaveCaptureLST()
        {
            foreach (DateTime startingTime in savedCapturesList.startCapture)
            {
                savedCapturesLST.Items.Add(startingTime.ToString(Clock.dateTimeFormatWithPoints));
            }

            // Select the last item
            //            savedCapturesLST.SelectedItem = lastElementCaptureTime.ToString();
        }


        private void savedCapturesLST_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox lb = (ListBox)sender;

            DateTime selectedDate = Convert.ToDateTime(lb.SelectedItem.ToString());


            for(int i = 0;i<savedCapturesList.startCapture.Count; i++)
            {
                if(savedCapturesList.startCapture[i].Equals(selectedDate))
                {
                    totalLength = savedCapturesList.endCapture[i] - savedCapturesList.startCapture[i];
                    totalLengthLBL.Content = totalLength;
                    break;
                }
            }



            if (movingViewModelDashboard.MovingPlotModel != null)
            {
                Graph.deleteAllLineSeries(movingViewModelDashboard.MovingPlotModel);
                movingViewModelDashboard.LoadData(selectedDate);
                MovingGraph.RefreshPlot(true);
            }

            if (gesturesViewModelDashboard.GesturesPlotModel != null)
            {
                Graph.deleteAllLineSeries(gesturesViewModelDashboard.GesturesPlotModel);
                gesturesViewModelDashboard.LoadData(selectedDate);
                GesturesGraph.RefreshPlot(true);
            }

            if (slidesChangeViewModelDashboard.SlidesChangePlotModel != null)
            {
                Graph.deleteAllLineSeries(slidesChangeViewModelDashboard.SlidesChangePlotModel);
                slidesChangeViewModelDashboard.LoadData(selectedDate);
                SlideChangeGraph.RefreshPlot(true);
            }

            if (videoDetectedViewModelDashboard.MultimediaPlotModel != null)
            {
                Graph.deleteAllLineSeries(videoDetectedViewModelDashboard.MultimediaPlotModel);
                videoDetectedViewModelDashboard.LoadData(selectedDate);
                MultimediaGraph.RefreshPlot(true);
            }


            if (WordsPerMinuteViewModelDashboard.WordsPerMinutePlotModel != null)
            {
                Graph.deleteAllLineSeries(WordsPerMinuteViewModelDashboard.WordsPerMinutePlotModel);
                WordsPerMinuteViewModelDashboard.LoadData(selectedDate);
                WordsPerMinuteGraph.RefreshPlot(true);
            }

            if (viewingDirectionViewModelDashboard.ViewingDirectionPlotModel != null)
            {
                Graph.deleteAllLineSeries(viewingDirectionViewModelDashboard.ViewingDirectionPlotModel);
                viewingDirectionViewModelDashboard.LoadData(selectedDate);
                ViewingDirectionGraph.RefreshPlot(true);
            }

        }



        private void GesturesPageBTN_Click(object sender, RoutedEventArgs e)
        {
            GesturesPage gesturesPage = new GesturesPage(databaseName);
            gesturesPage.initializeGesture();
            Switcher.Switch(gesturesPage);
        }

        private void MovementPageBTN_Click(object sender, RoutedEventArgs e)
        {
            MovementPage movementPage = new MovementPage(databaseName);
            movementPage.initializeMovement();
            Switcher.Switch(movementPage);
        }

        private void SlidesPageBTN_Click(object sender, RoutedEventArgs e)
        {
            SlidesPage slidesPage = new SlidesPage(databaseName);
            slidesPage.initializeSlides();
            Switcher.Switch(slidesPage);
        }

        private void ViewingDirectionPageBTN_Click(object sender, RoutedEventArgs e)
        {
            ViewingDirectionPage viewingDirectionPage = new ViewingDirectionPage(databaseName);
            viewingDirectionPage.initializeViewingDirection();
            Switcher.Switch(viewingDirectionPage);
        }


        private void DashboardPageBTN_Click(object sender, RoutedEventArgs e)
        {
            Dashboard dashboard = new Dashboard(databaseName);
            dashboard.initializeDashboard();
            Switcher.Switch(dashboard);
        }

        public void UtilizeState(object state)
        {
            throw new NotImplementedException();
        }
	}
}